package gw

import (
	"github.com/gin-gonic/gin"
	"github.com/shenyisyn/goft-gin/goft"
	"istio.io/client-go/pkg/apis/networking/v1alpha3"
	istio "istio.io/client-go/pkg/clientset/versioned"
	"k8s.io/apimachinery/pkg/apis/meta/v1"
)
type GateWayCtl struct {
	GwService *GateWayService  `inject:"-"`
	Client *istio.Clientset `inject:"-"`
}

func NewGateWayCtl() *GateWayCtl {
	return &GateWayCtl{}
}

func(this *GateWayCtl) SaveGateWay(c *gin.Context) goft.Json{
	gw:=&v1alpha3.Gateway{}
	goft.Error(c.ShouldBindJSON(gw))

	_,err:=this.Client.NetworkingV1alpha3().Gateways(gw.Namespace).Create(c,gw,v1.CreateOptions{})
	goft.Error(err)
	return gin.H{
		"code":20000,
		"data":"success",
	}
}
func(this *GateWayCtl) GwList(c *gin.Context) goft.Json{
	ns:=c.DefaultQuery("ns","default")
	return gin.H{
		"code":20000,
		"data":this.GwService.ListGW(ns),
	}
}
func(this *GateWayCtl) DeleteGW(c *gin.Context) goft.Json{
	ns:=c.Param("ns")
	name:=c.Param("name")
	goft.Error(this.Client.NetworkingV1alpha3().Gateways(ns).Delete(c,name,v1.DeleteOptions{}))
	return gin.H{
		"code":20000,
		"data":"success",
	}
}

//加载网关详细
func(this *GateWayCtl) LoadGW(c *gin.Context) goft.Json{
	ns:=c.Param("ns")
	name:=c.Param("name")

	return gin.H{
		"code":20000,
		"data":this.GwService.LoadGw(ns,name),
	}
}


func(*GateWayCtl) Name() string{
	return "GateWayCtl"
}


func(this *GateWayCtl)  Build(goft *goft.Goft){
	goft.Handle("GET","/gateways",this.GwList)
	goft.Handle("POST","/gateways",this.SaveGateWay)
	goft.Handle("GET","/gateways/:ns/:name",this.LoadGW)

	goft.Handle("DELETE","/gateways/:ns/:name",this.DeleteGW)

}